import Vue from 'vue'
import Loadings from './index.vue'

let LoadingConstructor = Vue.extend(Loadings)
const div = document.createElement('div')
document.body.appendChild(div)

class Loading {
  static isLoading = false
  static timer = null

  constructor(config = {}) {
    this.instance = new LoadingConstructor({ data: config })
    this.dom = this.instance.$mount().$el
  }
  start() {
    clearTimeout(this.timer)
    if (this.isLoading) return
    div.appendChild(this.dom)
    this.isLoading = true

  }
  close() {
    clearTimeout(this.timer)
    if (div.children.length == 0) return
    this.timer = setTimeout(() => {
      div.removeChild(this.dom)
      this.isLoading = false
    }, 300)
  }
}

export default new Loading()